package com.woke.photoactivity;

import java.util.ArrayList;
import java.util.List;

import com.zhongcai.online.R;


import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAdapter extends CommonAdapter<String>{

	/**
	 * �û�ѡ���ͼƬ���洢ΪͼƬ������·��
	 */
	public static ArrayList<String> mSelectedImage = new ArrayList<String>();

	/**
	 * �ļ���·��
	 */
	private String mDirPath;
	private int size = 0;
	private boolean isok = false;
	private TextView mTextView;
	private Context context;
	public String photoresult = "";
	private int parent;
	public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
			String dirPath, TextView mTextView, int parent){
		super(context, mDatas, itemLayoutId);
		
		this.context = context;
		this.mDirPath = dirPath;
		this.mTextView = mTextView;
		this.parent = parent;
		
	}
	
	
	
	public ArrayList<String> photolist(){
		return mSelectedImage;
	}

	@Override
	public void convert(final com.woke.photoactivity.ViewHolder helper, final String item){
		//����no_pic
		helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
		//����no_selected
				helper.setImageResource(R.id.id_item_select,
						R.drawable.picture_unselected);
		//����ͼƬ
		helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);
		
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		
		mImageView.setColorFilter(null);
		//����ImageView�ĵ���¼�
		mImageView.setOnClickListener(new OnClickListener(){
			

			//ѡ����ͼƬ�䰵����֮��֮
			@Override
			public void onClick(View v){
				
				
				if(parent == 10){
				if(photoresult.equals("") || null == photoresult){
					mSelect.setImageResource(R.drawable.pictures_selected);
					mImageView.setColorFilter(Color.parseColor("#77000000"));
					photoresult = mDirPath + "/" + item;
					mTextView.setText("确 定");
				}else{
					if(photoresult.equals(mDirPath + "/" + item)){
						mSelect.setImageResource(R.drawable.picture_unselected);
						mImageView.setColorFilter(null);
						mTextView.setText("请选中一张图片");
						photoresult = "";
					}else{
						Toast.makeText(context, "最多选择1张图片", Toast.LENGTH_SHORT).show();
					}
				}
					
				}
//				else if(parent < 4){
//					if(size < 3){
//						// �Ѿ�ѡ����ͼƬ
//						if (mSelectedImage.contains(mDirPath + "/" + item)){
//							mSelectedImage.remove(mDirPath + "/" + item);
//							mSelect.setImageResource(R.drawable.picture_unselected);
//							mImageView.setColorFilter(null);
//							size = mSelectedImage.size();
////							mTextView.setText(size+"/"+"4");
//							mTextView.setText("���"+"��"+size+"/3��");
//						} else{
//							// δѡ���ͼƬ
//							mSelectedImage.add(mDirPath + "/" + item);
//							mSelect.setImageResource(R.drawable.pictures_selected);
//							mImageView.setColorFilter(Color.parseColor("#77000000"));
//							size = mSelectedImage.size();
////							mTextView.setText(size+"/"+"4");
//							mTextView.setText("���"+"��"+size+"/3��");
//						}
//					}else{
//						if (mSelectedImage.contains(mDirPath + "/" + item)){
//							mSelectedImage.remove(mDirPath + "/" + item);
//							mSelect.setImageResource(R.drawable.picture_unselected);
//							mImageView.setColorFilter(null);
//							size = mSelectedImage.size();
////							mTextView.setText(size+"/"+"4");
//							mTextView.setText("���"+"��"+size+"/3��");
//						}else{
//							Toast.makeText(context, "���ѡ��3��ͼƬ", Toast.LENGTH_SHORT).show();
//						}
////						mTextView.setText("ȷ��");
//					}
//				}
				else{
					if(photoresult.equals("") || null == photoresult){
						mSelect.setImageResource(R.drawable.pictures_selected);
						mImageView.setColorFilter(Color.parseColor("#77000000"));
						photoresult = mDirPath + "/" + item;
						mTextView.setText("确 定");
					}else{
						if(photoresult.equals(mDirPath + "/" + item)){
							mSelect.setImageResource(R.drawable.picture_unselected);
							mImageView.setColorFilter(null);
							mTextView.setText("请选中一张图片");
							photoresult = "";
						}else{
							Toast.makeText(context, "最多选择1张图片", Toast.LENGTH_SHORT).show();
						}
					}
				}
				
				
				
				
				
//				if(size == 1){
//					// �Ѿ�ѡ����ͼƬ
//					if (mSelectedImage.contains(mDirPath + "/" + item)){
//						mSelectedImage.remove(mDirPath + "/" + item);
//						mSelect.setImageResource(R.drawable.picture_unselected);
//						mImageView.setColorFilter(null);
//						size = 0;
////						mTextView.setText(size+"/"+"4");
//						mTextView.setText("���(0/3)");
//					} else{
//						// δѡ���ͼƬ
//						mSelectedImage.add(mDirPath + "/" + item);
//						mSelect.setImageResource(R.drawable.pictures_selected);
//						mImageView.setColorFilter(Color.parseColor("#77000000"));
//						size = 1;
//						Toast.makeText(context, "���ѡ��1��ͼƬ", Toast.LENGTH_SHORT).show();
////						mTextView.setText(size+"/"+"4");
//						mTextView.setText("���");
//					}
//				}else if(size == 0){
//					if (mSelectedImage.contains(mDirPath + "/" + item)){
//						mSelectedImage.remove(mDirPath + "/" + item);
//						mSelect.setImageResource(R.drawable.picture_unselected);
//						mImageView.setColorFilter(null);
//						size = mSelectedImage.size();
////						mTextView.setText(size+"/"+"4");
//						mTextView.setText("���"+"��"+size+"/4��");
//					}else{
//						Toast.makeText(context, "���ѡ��4��ͼƬ", Toast.LENGTH_SHORT).show();
//					}
////					mTextView.setText("ȷ��");
//				}
				

			}
		});
		
		/**
		 * �Ѿ�ѡ����ͼƬ����ʾ��ѡ����Ч��
		 */
		if (mSelectedImage.contains(mDirPath + "/" + item)){
			mSelect.setImageResource(R.drawable.pictures_selected);
			mImageView.setColorFilter(Color.parseColor("#77000000"));
		}

	}

}
